package com.sgb.member.view_model;

import android.text.TextUtils;

import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.entity.MemberLogisticsEntity;
import com.sgb.member.entity.MemberServiceListBean;
import com.sgb.member.ui.fragment.BusinessEnterpriseFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.areapopup.GoodsSourceLocationEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BusinessEnterpriseMode extends BaseViewModel {

    private final BusinessEnterpriseFragment fragment;
    private final MemberRepertory mRepertory;
    private final int maxPageSize = 10;
    private int current;

    private CommonPopEntity mAll;

    public BusinessEnterpriseMode(BusinessEnterpriseFragment fragment, MemberRepertory repertory) {
        this.fragment = fragment;
        this.mRepertory = repertory;

        mAll = new CommonPopEntity();
        mAll.setNo("");
        mAll.setTypeName("全部");
    }

    public void showOrHideDialog(boolean aBoolean) {
        if (aBoolean) {
            if (!DialogHelper.isShowing()) {
                DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...");
            }
        } else {
            DialogHelper.dismissProgressDialog();
            fragment.closeRecyclerRefreshing();
        }
    }

    /**
     * 获取省市区地址
     */
    public void getAddressModel() {
        showOrHideDialog(true);
        mRepertory.getApi().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                showOrHideDialog(false);
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setAddressData(data.getData());
                }
            }
        });
    }

    public void getEnterpriseServiceList(boolean isLoadMore, String serviceName,
                                         String serviceClass, String serviceClass2,
                                         String serviceClass3, GoodsSourceLocationEntity entity,
                                         String level) {
        int currentPage = isLoadMore ? current + 1 : 1;
        showOrHideDialog(true);
        Map<String, Object> map = new HashMap<>();
        map.put("pageSize", currentPage);
        map.put("limit", maxPageSize);
        map.put("asc", false);
        map.put("sortType", 1);
        map.put("serviceClass", serviceClass);
        map.put("serviceCategory", serviceClass2);
        map.put("serviceItem", serviceClass3);
        map.put("serviceName", serviceName);
        map.put("level", level);
        List<String> codes = new ArrayList<>();
        if (entity != null) {
            if (!TextUtils.isEmpty(entity.getProvinceNo())) {
                codes.add(entity.getProvinceNo());
            }
            if (!TextUtils.isEmpty(entity.getCityNo())) {
                codes.add(entity.getCityNo());
            }
        }
        map.put("provinceCode", codes);
        map.put("type", 1); // 发布类型 1 企业 ；2 团队
        mRepertory.getApi().getServiceHallCompanyOrTeamList(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<MemberServiceListBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<MemberServiceListBean>>> data) {
                current = currentPage;
                int total = data.getData().getPages();
                fragment.refreshRecycler(data.getData().getList(), isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<MemberServiceListBean>>>> call, Throwable t) {
                super.onFailure(call, t);
                if (!isLoadMore) {
                    fragment.showEmpty();
                }
            }
        });
    }

    public void getMemberLevelData(int type, boolean isShowLeftDialog) {
        mRepertory.getApi().getMemberLevelList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    if (isShowLeftDialog) {
//                        fragment.showLeftDialog(type, data.getData());
                    } else {
                        fragment.setChooseItemData(type, data.getData(), false);
                    }
                }
            }
        });
    }

}
